package dev.browse;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;

public class TabbedPaneWebBrowser extends JFrame {
	private static final long serialVersionUID = -2888631593600983224L;
	private JTabbedPane tabbedPane = new JTabbedPane();

	public TabbedPaneWebBrowser() {
		super("Browser");
		System.setProperty("http.agent", UserAgent.userAgent());
		createNewTab();

		getContentPane().add(tabbedPane);

		JMenu fileMenu = new JMenu("File");
		fileMenu.add(new NewTabAction());
		fileMenu.addSeparator();
		fileMenu.add(new ExitAction());
		fileMenu.setMnemonic('F');

		JMenuBar menuBar = new JMenuBar();
		menuBar.add(fileMenu);
		setJMenuBar(menuBar);

	}

	private void createNewTab() {
		JPanel panel = new JPanel(new BorderLayout());
		WebBrowserPane browserPane = new WebBrowserPane();
		
		WebToolBar toolBar = new WebToolBar(browserPane, panel);
		panel.add(toolBar, BorderLayout.NORTH);
		panel.add(new JScrollPane(browserPane), BorderLayout.CENTER);
		tabbedPane.addTab("Browser " + tabbedPane.getTabCount(), panel);
	}

	private class NewTabAction extends AbstractAction {
		private static final long serialVersionUID = 5523249405571907806L;

		public NewTabAction() {
			putValue(Action.NAME, "New Tab");
			putValue(Action.SHORT_DESCRIPTION, "Create New Web Browser Tab");
			putValue(Action.MNEMONIC_KEY, new Integer('N'));
		}

		public void actionPerformed(ActionEvent event) {
			createNewTab();
		}
	}

	private class ExitAction extends AbstractAction {
		private static final long serialVersionUID = 8488831772601719278L;

		public ExitAction() {
			putValue(Action.NAME, "Exit");
			putValue(Action.SHORT_DESCRIPTION, "Exit Application");
			putValue(Action.MNEMONIC_KEY, new Integer('x'));
		}

		public void actionPerformed(ActionEvent event) {
			System.exit(0);
		}
	}


	public static void main(String args[]) {
		TabbedPaneWebBrowser browser = new TabbedPaneWebBrowser();
		browser.setDefaultCloseOperation(EXIT_ON_CLOSE);
		browser.setSize(800, 600);
		browser.setVisible(true);
	}
}
